package images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;

public class PicsResize {
    public static final String BASE_FOLDER = "images/extramobi/";
//    public static final String BASE_FOLDER = "images/cellmart/";
    public static final String LOGO_IMAGE_FILE = BASE_FOLDER + "gen/logo1.jpg";
    public static final String PICS_IMPORT_FOLDER = BASE_FOLDER + "products_pictures/";
//    public static final String PICS_IMPORT_FOLDER = BASE_FOLDER + "dmitry/products_pictures/";
    //    public static final String PICS_IMPORT_FOLDER = "C:\\denwer\\home\\localhost\\www\\webasyst\\published\\publicdata\\WEBASYST\\attachments\\SC\\products_pictures\\";
    public static final String GENERAL_IMPORT_FOLDER = BASE_FOLDER + "gen/";
    public static final String CAT_FOLDER = BASE_FOLDER + "cat/";
    public static final String PICS_SOURCE_PATH = BASE_FOLDER + "";

    public static java.util.List<String> productPictures;

    public static void main(String[] args) throws IOException {
        List<String> exceptionFolders = new ArrayList<String>();
        exceptionFolders.add("accum");
        exceptionFolders.add("charger");
        exceptionFolders.add("body");
        exceptionFolders.add("plenki");
        File importFolder = new File(PICS_IMPORT_FOLDER);
        File catFolder = new File(CAT_FOLDER);
        File genFolder = new File(GENERAL_IMPORT_FOLDER);
        if (!importFolder.exists()) importFolder.mkdir();
        for (File folder : new File(PICS_SOURCE_PATH).listFiles()) {
            if (!folder.equals(importFolder) && !folder.equals(catFolder) && !folder.equals(genFolder) && !folder.getName().contains(".svn") && !exceptionFolders.contains(folder.getName())) {
                resizeImages(folder, importFolder);
                System.out.println(folder + " resized!");
            } else if (folder.equals(catFolder) && !folder.getName().startsWith(".svn")) {
                PicsClone.copyFiles(CAT_FOLDER, PICS_IMPORT_FOLDER, true, "");
                System.out.println(folder + " copied!");
            }
        }
//        resizeImages("images/bodies");
//        resizeImages("images/accums/htc");
//        resizeImages("images/accums/samsung");
//        resizeImages("images/accums/iphone");
//        resizeImages("images/phones");
//        resizeImages("images/chargers");
//        resizeImages("images/craftmann");
//        resizeImages("images/headset");
//        resizeImages("images/glory");
    }

//    private static void copyUnmodified(File fromPath, File toPath) throws IOException {
//        PicsClone.copyFiles(fromPath.getPath(), toPath.getPath(), true, "");
//    }

    public static java.util.List<String> getProductPictures() {
        if (productPictures == null) {
            File picsImportFolder = new File(PICS_IMPORT_FOLDER);
            if (picsImportFolder.list() == null) {
                return new ArrayList<String>();
            }
            productPictures = Arrays.asList(picsImportFolder.list());
        }
        return productPictures;
    }

    private static void resizeImages(File fromPath, File destPath) throws IOException {
        if (fromPath.isFile()) {
            resizeImage(destPath, fromPath);
        } else {
            File[] images = fromPath.listFiles();
            for (File imageFile : images) {
                if (imageFile.getName().contains(".svn")) {
                    continue;
                }
                if (imageFile.isDirectory()) {
                    resizeImages(imageFile, destPath);
                    continue;
                }
                resizeImage(destPath, imageFile);
            }
        }
    }

    private static void resizeImage(File destPath, File imageFile) throws IOException {
        String name = imageFile.getName().replace(".", ",").split(",")[0];
        if (name.contains("-small") || name.contains("-medium") || name.contains("-large")) {
            return;
        }
        String ext = imageFile.getName().substring(imageFile.getName().lastIndexOf(".") + 1, imageFile.getName().length());
        if (!ext.equals("jpg")) {
            System.out.println("WARN: " + imageFile.getName() + " has unknown extention.");
            return;
        }
        BufferedImage image = ImageIO.read(imageFile);
        int newWidth = image.getWidth();
        if (newWidth > 600) {
            newWidth = 600;
        } else if (newWidth > 320) {
            newWidth = 320;
        }
        int newHeight = newWidth * image.getHeight() / image.getWidth();
//        int newWidth = image.getWidth();
//        int newHeight = image.getHeight();
        BufferedImage resizedImage = resize(image, newWidth, newHeight);
        BufferedImage overlayedImage = overlayWithPhone(resizedImage);
        ImageIO.write(overlayedImage, "jpg", new File(destPath + "\\" + name + "-large" + "." + ext));
//        newWidth = newWidth / 2;
//        newHeight = newWidth * image.getHeight() / image.getWidth();
        newWidth = 150;
        newHeight = newWidth * image.getHeight() / image.getWidth();
        resizedImage = resize(image, newWidth, newHeight);
        ImageIO.write(resizedImage, "jpg", new File(destPath + "\\" + name + "-medium" + "." + ext));
//        newWidth = newWidth / 2;
//        newHeight = newWidth * image.getHeight() / image.getWidth();
        newWidth = 80;
        newHeight = newWidth * image.getHeight() / image.getWidth();
        resizedImage = resize(image, newWidth, newHeight);
        ImageIO.write(resizedImage, "jpg", new File(destPath + "\\" + name + "-small" + "." + ext));
    }

//    public static void resizeImages(String path) throws IOException {
//        File folder = new File(path);
//        File[] images = folder.listFiles();
//        for (File imageFile : images) {
//            if (imageFile.isDirectory()) {
//                resizeImages(imageFile.getPath());
//                continue;
//            }
//            String name = imageFile.getName().replace(".", ",").split(",")[0];
//            if (name.contains("-small") || name.contains("-medium") || name.contains("-large")) {
//                continue;
//            }
//            String ext = imageFile.getName().substring(imageFile.getName().lastIndexOf(".") + 1, imageFile.getName().length());
//            if (!ext.equals("jpg")) {
//                System.out.println("WARN: " + imageFile.getName() + " has unknown extention.");
//                continue;
//            }
//            BufferedImage image = ImageIO.read(imageFile);
//            int newWidth = 312;
//            int newHeight = newWidth * image.getHeight() / image.getWidth();
//            BufferedImage resizedImage = overlayWithPhone(resize(image, newWidth, newHeight));
//            ImageIO.write(resizedImage, "jpg", new File(path + "\\" + name + "-large" + "." + ext));
//            newWidth = newWidth / 2;
//            newHeight = newWidth * image.getHeight() / image.getWidth();
//            resizedImage = overlayWithPhone(resize(image, newWidth, newHeight));
//            ImageIO.write(resizedImage, "jpg", new File(path + "\\" + name + "-medium" + "." + ext));
//            newWidth = newWidth / 2;
//            newHeight = newWidth * image.getHeight() / image.getWidth();
//            resizedImage = overlayWithPhone(resize(image, newWidth, newHeight));
//            ImageIO.write(resizedImage, "jpg", new File(path + "\\" + name + "-small" + "." + ext));
//        }
//    }

    private static BufferedImage overlayWithPhone(BufferedImage image) throws IOException {
        BufferedImage phoneImage = ImageIO.read(new File(LOGO_IMAGE_FILE));
        int newPhoneImageWidth = image.getWidth();
        int newPhoneImageHeight = newPhoneImageWidth * phoneImage.getHeight() / phoneImage.getWidth();
        phoneImage = resize(phoneImage, newPhoneImageWidth, newPhoneImageHeight);
        BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight() + phoneImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = newImage.createGraphics();
        g.drawImage(phoneImage, 0, 0, null);
        g.drawImage(image, 0, phoneImage.getHeight(), null);
        float alpha = 0.6f;
        int type = AlphaComposite.SRC_OVER;
        AlphaComposite composite =
                AlphaComposite.getInstance(type, alpha);
        g.setComposite(composite);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawImage(phoneImage, 0, image.getHeight() / 2, null);
        g.dispose();
        return newImage;
    }

    private static BufferedImage resize(BufferedImage image, int newWidth, int newHeight) {
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(image, 0, 0, newWidth, newHeight, null);
        g.dispose();
        return resizedImage;
    }
}
